package com.xm.xmds1705083.service.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xm.xm.dto.ResultDto;
import com.xm.xm.dto.SearchDto;
import com.xm.xm.util.DateUtils;
import com.xm.xm.util.ErrorCode;
import com.xm.xm.util.ValidateUtils;
import com.xm.xmds1705083.service.mapper.DuoduoSmsMapper;
import com.xm.xmds1705083.service.mapper.DuoduoUserCarMapper;
import com.xm.xmds1705083.service.mapper.DuoduoUserSpaceMapper;
import com.xm.xmds1705083.service.model.DuoduoSms;
import com.xm.xmds1705083.service.model.DuoduoUserCar;
import com.xm.xmds1705083.service.model.DuoduoUserSpace;
import com.xm.xmds1705083.service.service.DuoduoUserCarService;
import com.xm.xmds1705083.service.util.GlobalError;
import net.sf.json.JSONObject;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 杭州新苗网络科技有限公司
 *
 * @author meihf
 * @date 2017/06/15
 * @description
 */
@Service("duoduoUserCarService")
public class DuoduoUserCarServiceImpl extends ServiceImpl<DuoduoUserCarMapper, DuoduoUserCar> implements DuoduoUserCarService {

    @Autowired
    DuoduoUserCarMapper duoduoUserCarMapper;

    @Autowired
    DuoduoSmsMapper duoduoSmsMapper;

    @Autowired
    DuoduoUserSpaceMapper duoduoUserSpaceMapper;

    @Override
    public ResultDto findMyMediaNo(SearchDto searchDto) {
        ResultDto resultDto = new ResultDto();
        searchDto.setPageNo((searchDto.getPageNo() - 1) * searchDto.getPageSize());
        try {
            EntityWrapper entityWrapper = new EntityWrapper();
            entityWrapper.where(" userId={0} and isDelete=0", searchDto.getAppUserId());
            List<DuoduoUserCar> userCarList = duoduoUserCarMapper.selectList(entityWrapper);
            if (userCarList.size() == 0) {
                resultDto.setCode(ErrorCode.C11001);
                resultDto.setMsg(ErrorCode.E11001);
                return resultDto;
            } else {
                resultDto.setData(userCarList);
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            resultDto.setCode(ErrorCode.C10013);
            resultDto.setMsg(ErrorCode.E10013);
        }
        return resultDto;
    }


   @Override
   public ResultDto saveMyMediaNo(SearchDto searchDto) {
       ResultDto resultDto = new ResultDto();
       JSONObject filed = searchDto.getFiled();
       searchDto.setPageNo((searchDto.getPageNo() - 1) * searchDto.getPageSize());
       try {
           if (!filed.has("mediaNo")) {
               resultDto.setMsg(ErrorCode.E10011);
               resultDto.setCode(ErrorCode.C10011);
               return resultDto;
           } else {
               Long phone = filed.getLong("phone");
               String mediaNo = filed.getString("mediaNo");
               //先查询是否库中已有
              /* EntityWrapper entityWrapper = new EntityWrapper();
               entityWrapper.where(" phone={0} and isDelete=0", phone);
               int resultSelect = duoduoUserCarMapper.selectCount(entityWrapper);
               if (ValidateUtils.isGreaterThan0(resultSelect)) {
                   resultDto.setCode(GlobalError.C11019);
                   resultDto.setMsg(GlobalError.E11019);
               } else {*/
                   DuoduoUserCar duoduoUserCar = new DuoduoUserCar();
                   duoduoUserCar.setUserId(searchDto.getAppUserId().intValue());
                   duoduoUserCar.setMediaNo(mediaNo);
                   duoduoUserCar.setPhone(String.valueOf(phone));
                   int insertResult = duoduoUserCarMapper.insert(duoduoUserCar);
                   if (ValidateUtils.isGreaterThan0(insertResult)) {
                       resultDto.setMsg("添加成功");
                   } else {
                       resultDto.setMsg(ErrorCode.E11004);
                       resultDto.setCode(ErrorCode.C11004);
                   }
               }

       } catch (RuntimeException e) {
           e.printStackTrace();
           resultDto.setCode(ErrorCode.C10013);
           resultDto.setMsg(ErrorCode.E10013);
       }
       return resultDto;
   }



    @Override
    public ResultDto deleteMyMediaNo(SearchDto searchDto) {
        ResultDto resultDto = new ResultDto();
        try {
            EntityWrapper entityWrapper = new EntityWrapper();
            entityWrapper.where(" userId={0} and isDelete=0 ",searchDto.getAppUserId());
            List<DuoduoUserCar> userCarList = duoduoUserCarMapper.selectList(entityWrapper);
            DuoduoUserCar duoduoUserCar = userCarList.get(0);
            duoduoUserCar.setIsDelete(1);
            int result = duoduoUserCarMapper.updateById(duoduoUserCar);
            if (ValidateUtils.isGreaterThan0(result)){
                resultDto.setMsg("删除成功");
            }else {
                resultDto.setMsg(ErrorCode.E11002);
                resultDto.setCode(ErrorCode.C11002);
            }
        }catch (RuntimeException e ){
            e.printStackTrace();
            resultDto.setCode(ErrorCode.C10013);
            resultDto.setMsg(ErrorCode.E10013);
        }
        return resultDto;
    }
    public  ResultDto certfMyMediaNo(SearchDto searchDto){
        ResultDto resultDto = new ResultDto();
        searchDto.setPageNo((searchDto.getPageNo() - 1) * searchDto.getPageSize());
        try {
            EntityWrapper entityWrapper1 = new EntityWrapper();
            entityWrapper1.where("userId={0} and isDelete=0",searchDto.getAppUserId());//searchDto.getAppUserId()
             int count=duoduoUserCarMapper.selectCount(entityWrapper1);
            if (ValidateUtils.isGreaterThan0(count)){
                resultDto.setCode(GlobalError.C11031);
                resultDto.setMsg(GlobalError.E11031);
            }else{
                resultDto.setCode(GlobalError.C11039);
                resultDto.setMsg(GlobalError.E11039);
            }


        } catch (RuntimeException e) {
            e.printStackTrace();
            resultDto.setCode(ErrorCode.C10013);
            resultDto.setMsg(ErrorCode.E10013);
        }
        return resultDto;
    }


    public  ResultDto certfIssueMyMediaNo(SearchDto searchDto){
        ResultDto resultDto = new ResultDto();
        searchDto.setPageNo((searchDto.getPageNo() - 1) * searchDto.getPageSize());
        JSONObject filed = searchDto.getFiled();
        try {
            if (!filed.has("parkingSpace")){
                resultDto.setMsg(ErrorCode.E10011);
                resultDto.setCode(ErrorCode.C10011);
                return resultDto;
            }else {
                String parkingSpace = filed.getString("parkingSpace");
                EntityWrapper entityWrapper1 = new EntityWrapper();
                entityWrapper1.where("userId={0} and  parkingSpace={1} and isDelete=0",searchDto.getAppUserId(), parkingSpace);//searchDto.getAppUserId()
                List<DuoduoUserSpace> lists = duoduoUserSpaceMapper.selectList(entityWrapper1);
                if (lists.size()==0) {
                    resultDto.setCode(ErrorCode.C11001);
                    resultDto.setMsg(ErrorCode.E11001);
                    return resultDto;
                } else {
                    if (lists.get(0).getIsRelease()==0) {
                        resultDto.setCode(GlobalError.C11033);
                        resultDto.setMsg(GlobalError.E11033);
                        return resultDto;
                    }else if (lists.get(0).getIsRelease()==1) {
                        resultDto.setCode(GlobalError.C11034);
                        resultDto.setMsg(GlobalError.E11034);
                        return resultDto;
                    }

                        resultDto.setCode(GlobalError.C11035);
                        resultDto.setMsg(GlobalError.E11035);

                }
            }
         } catch (RuntimeException e) {
            e.printStackTrace();
            resultDto.setCode(ErrorCode.C10013);
            resultDto.setMsg(ErrorCode.E10013);
        }
        return resultDto;
    }

}

